
#汽车基本数据
import bs4
import requests
from bs4 import BeautifulSoup
import csv
import pandas
url="https://hefei.taoche.com/all/?page={}"
header={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
}
with open("二手车基本信息.csv","w",encoding="utf-8",newline="") as file:
    f=csv.writer(file)
    f.writerow(["标题","年份","里程(万公里)","城市","价格(万元)","标签","是否保修","详情页"])
    for page in range(1, 51):
        print(f'======================正在采集第{page}页的数据内容======================')
        resp=requests.get(url=url.format(page),headers=header)
    # print(resp)
        soup=BeautifulSoup(resp.content,"html.parser")
    # print(soup)
        big=soup.find("ul",attrs={"class":"gongge_ul"})
        for i in big:
            if type(i) is not bs4.NavigableString:
                a_all=i.find("a",attrs={"class":"title"})
                title=a_all.get("title")    #标题
                href=a_all.get("href")      #详情页
                a=i.find("p").text.replace("|","").replace("\n","")
                year = a[0:6]              #年份
                gls = a[30:34]             #里程
                city = a[87:93]            #城市
                price=i.find("i",attrs={"class":"Total brand_col"}).text    #价格
                price=price.replace("万"," ")
                # bq=i.find("em",attrs={"class":"qgg_tag"})    #标签
                if i.find("em",attrs={"class":"qgg_tag"})==None:
                    bq =None
                else:
                    bq=i.find("em",attrs={"class":"qgg_tag"}).text
                # bx=i.find("i",attrs={"class":"tc_label"})  #保修
                if i.find("i",attrs={"class":"tc_label"})==None:
                    bx = None
                else:
                    bx=i.find("i",attrs={"class":"tc_label"}).text
                f.writerow([title,year,gls,city,price,bq,bx,href])

















